<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never" v-loading="loading">
        <!-- 搜索栏 -->
        <el-form
          :inline="true"
          :model="pagination"
          ref="pagination"
        >
          <el-form-item label="用户名：" prop="username">
            <el-input v-model="pagination.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="姓名：" prop="username">
            <el-input v-model="pagination.realname" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="手机号：" prop="phone">
            <el-input v-model="pagination.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleRest">重置</el-button>
          </el-form-item>
        </el-form>
<!--        &lt;!&ndash; 搜索栏 / &ndash;&gt;-->
<!--        <div class="btnBox" style="text-align:right">-->
<!--          <el-button type="primary" @click="handleAdd">新增</el-button>-->
<!--        </div>-->
        <!-- 数据表格 -->
        <el-table
          :data="items"
          border
          style="width: 100%; margin-top:10px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="index" prop="id" label="编号" width="50"></el-table-column>
          <el-table-column prop="username" label="用户名称"></el-table-column>
          <el-table-column prop="realname" label="姓名"></el-table-column>
          <el-table-column prop="phone" label="手机号"></el-table-column>
          <el-table-column prop="isAdmin" label="是否为管理员">

          </el-table-column>

          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button @click="handleEdit(1,scope.row)" type="text" size="small">密码重置</el-button>
              <el-button @click="handleEdit(2,scope.row)" type="text" size="small">锁定</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
        ></el-pagination>
        <!-- 数据表格 / -->
      </el-card>
      <!-- 新增标签弹层 -->
      <Dialog ref="addType" :dialogTitle="dialogTitle" v-on:newDataes = subMitForm></Dialog>
    </div>
  </div>
</template>

<script>
import { getList, getDetails, delData } from '@/api/user'
import {getStoreUserList, editUserData} from '@/api/store'
import Dialog from './../components/userAdd'
export default {
  name: 'userList',
  components: {
    Dialog
  },
  data() {
    return {
      items: [
        {
        }
      ],
      total: 20,
      pagination: {
        page: 1,
        pageSize: 10,
        username: '',
        realname: '',
        phone: ''
      },
      loading: false,
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: ''
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    // 业务方法
    async doQuery(parent) {
      this.loading = true
      await getStoreUserList({...parent, storeId: this.$route.query.id})
        .then(res => {
          const { data } = res
          this.items = data.items
          this.total = data.counts
          this.loading = false
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
    },
    // UI方法 - 重置
    handleRest() {
      this.$refs['pagination'].resetFields()
    },
    // 搜索
    handleSearch() {
      this.doQuery(this.pagination)
    },
    //
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    // 每页条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagination.pageSize = val
      this.doQuery(this.pagination)
    },
    // 当前页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pagination.page = val
      this.doQuery(this.pagination)
    },
    // 添加
    handleAdd(val) {
      this.dialogTitle = '添加'
      this.$refs.addType.dialogFormV()
    },
    // 详情获取  - 如果有详情接口 读详情
    async getDetails(id) {
      let dataes = {}
      await getDetails(id)
        .then(res => {
          const { data } = res
          this.loading = false
          dataes = data
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
      return dataes
    },
    // 密码重置
    // 锁定
    async handleEdit (st, val) {
      // 如果有详情接口 读详情
      if (st === 1) {
        this.dialogTitle = '更改密码'
        this.$refs.addType.dialogFormV(val)
      } else {
        if (val.status === 2) {
          this.$message.error('用户已处于锁定状态')
        }
        this.$confirm('是否确认锁定？')
          .then(async ret => {
            const {data} = await editUserData({id: val.id, status: 2})
            if (data.message) {
              this.$message.error(data.message)
            } else {
              this.$message.success('状态更新成功')
            }
          })
          .catch(err => {
            this.$message.error(err)
            this.loading = false
          })
      }
    },
    // 删除
    handleDelect(item) {
      this.$confirm('是否确认删除？')
        .then(async ret => {
          await delData(item.id)
            .then(res => {
              this.loading = false
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.doQuery(this.pagination)
            })
            .catch(err => {
              console.log(err)
              this.loading = false
            })
        })
        .catch(ret => {
          console.log(ret)
        })
    },
    // subMitForm
    subMitForm(val) {
      if (val.id) {
        this.$message.success('修改成功')
      } else {
        this.$message.success('添加成功')
      }
      this.doQuery(this.pagination)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px 0px 0px;
}
.pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
